package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import util.Datautil;


import model.Thanhvienmodel;


public class Thanhviendao {
 public static Thanhvienmodel getThanhVien(String TENDN, String MATKHAU){
	 Thanhvienmodel model= null;
	 try {
		String sql ="Select * from THANHVIEN Where TENDN ='"
			 +TENDN+"' and MATKHAU ='"
			 +MATKHAU+"'";
		 ResultSet rs=Datautil.executeQuery(sql);
		 while (rs.next()){
			 model = new Thanhvienmodel();
			 model.setID(rs.getString("ID"));
			 model.setTENDN(rs.getString("TENDN"));
			 model.setMATKHAU(rs.getString("MATKHAU"));
			 model.setVAITRO(rs.getString("VAITRO"));
			 model.setGHICHU(rs.getString("GHICHU"));
		 }
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	 
	 return model;
	 
	 
 }

 public static Thanhvienmodel getThanhVienByID(String id){
	 Thanhvienmodel model= null;
	 try {
		String sql  = "SELECT * FROM THANHVIEN WHERE ID=?";
		PreparedStatement ps = Datautil.getConnection().prepareStatement(sql);
		ps.setString(1, id);
		
		 ResultSet rs= ps.executeQuery();
		 while (rs.next()){
			 model = new Thanhvienmodel();
			 model.setID(rs.getString("ID"));
			 model.setTENDN(rs.getString("TENDN"));
			 model.setMATKHAU(rs.getString("MATKHAU"));
			 model.setVAITRO(rs.getString("VAITRO"));
			 model.setGHICHU(rs.getString("GHICHU"));
		 }
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	 
	 return model;
	 
	 
 }
 
 
 public static ArrayList<Thanhvienmodel> getallThanhvien(){
	ArrayList<Thanhvienmodel> listThanhvien=new ArrayList<Thanhvienmodel>();
	try {
		String sql="select * from THANHVIEN";
		ResultSet rs =Datautil.executeQuery(sql);
		while(rs.next()){
			Thanhvienmodel model=new Thanhvienmodel();
			model.setID(rs.getString("ID"));
			model.setTENDN(rs.getString("TENDN"));
			model.setMATKHAU(rs.getString("MATKHAU"));
			model.setVAITRO(rs.getString("VAITRO"));
			model.setGHICHU(rs.getString("GHICHU"));
			listThanhvien.add(model);
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}
	return listThanhvien;
	
	
}
 //xu ly them moi thanh vien
 public static Boolean Themthanhvien(Thanhvienmodel model){

	 
	 Boolean rs=false;
	 try {
		String sql="INSERT INTO THANHVIEN(tendn,matkhau,vaitro,ghichu) VALUES(?, ?, ?, ?)";
		 PreparedStatement ps=Datautil.getConnection().prepareStatement(sql);
		 ps.setString(1, model.getTENDN());
		 ps.setString(2,model.getMATKHAU());
		 ps.setString(3, model.getVAITRO());
		 ps.setString(4, model.getGHICHU());
		 ps.executeUpdate();
		 rs=true;
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return rs;
	 
 }

//xu ly update
 
 public static Boolean updateThanhVien(Thanhvienmodel model) {
	 Boolean kq = false;
	 
	 try {
		String sql ="UPDATE THANHVIEN SET TENDN=?, MATKHAU=?, VAITRO=?,GHICHU=? WHERE ID=?";
		PreparedStatement ps = Datautil.getConnection().prepareStatement(sql);
		ps.setString(1, model.getTENDN());
		ps.setString(2, model.getMATKHAU());
		ps.setString(3, model.getVAITRO());
		ps.setString(4, model.getGHICHU());
		ps.setString(5, model.getID());
		ps.executeUpdate();
		kq = true;
	} catch (Exception e) {
		e.printStackTrace();
	}
	 
	 return kq;
 }
}

